package com.beyond.trace.dy.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.beyond.trace.dy.domain.TraceDyProduct;
import com.beyond.trace.dy.service.ITraceDyProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 产品管理Controller
 * 
 * @author Mr.Yao
 * @date 2022-04-08
 */
@RestController
@RequestMapping("/dy/product")
@Api(value = "产品管理", tags = "产品管理")
public class TraceDyProductController extends BaseController
{
    @Autowired
    private ITraceDyProductService traceDyProductService;

    //@PreAuthorize("@ss.hasPermi('dy:product:list')")
    @GetMapping("/list")
    @ApiOperation("查询产品管理列表")
    public TableDataInfo list(TraceDyProduct traceDyProduct)
    {
        startPage();
        List<TraceDyProduct> list = traceDyProductService.selectTraceDyProductList(traceDyProduct);
        return getDataTable(list);
    }

    //@PreAuthorize("@ss.hasPermi('dy:product:query')")
    @GetMapping(value = "/query")
    @ApiOperation("查询产品管理列表不分页")
    public AjaxResult getInfoQuery(TraceDyProduct traceDyProduct)
    {
        return AjaxResult.success(traceDyProductService.selectTraceDyProductList(traceDyProduct));
    }

    //@PreAuthorize("@ss.hasPermi('dy:product:export')")
    @Log(title = "产品管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TraceDyProduct traceDyProduct)
    {
        List<TraceDyProduct> list = traceDyProductService.selectTraceDyProductList(traceDyProduct);
        ExcelUtil<TraceDyProduct> util = new ExcelUtil<TraceDyProduct>(TraceDyProduct.class);
        return util.exportExcel(list, "产品管理数据");
    }

    //@PreAuthorize("@ss.hasPermi('dy:product:query')")
    @GetMapping(value = "/{productId}")
    @ApiOperation("查询产品管理列表")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return AjaxResult.success(traceDyProductService.selectTraceDyProductById(productId));
    }

    //@PreAuthorize("@ss.hasPermi('dy:product:add')")
    @Log(title = "产品管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增产品管理")
    public AjaxResult add(@RequestBody TraceDyProduct traceDyProduct)
    {
        return toAjax(traceDyProductService.insertTraceDyProduct(traceDyProduct));
    }

    //@PreAuthorize("@ss.hasPermi('dy:product:edit')")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改产品管理")
    public AjaxResult edit(@RequestBody TraceDyProduct traceDyProduct)
    {
        return toAjax(traceDyProductService.updateTraceDyProduct(traceDyProduct));
    }

    //@PreAuthorize("@ss.hasPermi('dy:product:remove')")
    @Log(title = "产品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    @ApiOperation("删除产品管理")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(traceDyProductService.deleteTraceDyProductByIds(productIds));
    }

    @Log(title = "图片上传", businessType = BusinessType.INSERT)
    @PostMapping("/picture/upload")
    @ApiOperation("图片上传接口")
    public AjaxResult avatar(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            if (!file.isEmpty()) {
                String avatar = FileUploadUtils.upload(RuoYiConfig.getProfile()+"/dy/product", file);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("path", avatar);
                return ajax;
            } else {
                return AjaxResult.error("上传图片异常，请联系管理员");
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
